package com.ysu.zyw.web;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;
import org.springframework.web.servlet.view.RedirectView;

import com.ysu.zyw.entity.User;
import com.ysu.zyw.svc.ActivitiService;
import com.ysu.zyw.svc.UserService;
import com.ysu.zyw.tool.ThreeTuple;
import com.ysu.zyw.tool.Tuple;
import com.ysu.zyw.web.vo.HistoricTaskVo;
import com.ysu.zyw.web.vo.ReimbursementVo;
import com.ysu.zyw.web.vo.TaskVo;

@Controller
public class ActivitiController {

    @SuppressWarnings("unused")
    private static final Logger logger = LoggerFactory.getLogger(ActivitiController.class);

    @Resource
    private ActivitiService activitiService;

    @Resource
    private UserService userService;

    @RequestMapping(value = "/")
    public ModelAndView Index() {
        return new ModelAndView(new RedirectView("/viewDeployment"));
    }

    @RequestMapping(value = "/deploy")
    public ModelAndView deployProcess() {
        activitiService.deploy();
        return new ModelAndView(new RedirectView("/viewDeployment"));
    }

    /**
     * 获取流程部署的数量/流程定义的数量/流程部署/流程部署对应的资源名列表/流程部署对应的流程定义列表
     */
    @RequestMapping(value = "/viewDeployment")
    public ModelAndView viewDeployment() {
        List<User> userList = userService.getAllUser();
        Tuple<Long, Long> deploymentCount = activitiService.getActivitiDeploymentCount();
        List<ThreeTuple<Deployment, List<String>, List<ProcessDefinition>>> deploymentList = activitiService
                .getActivitiDeploymentList();
        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("deploymentCount", deploymentCount);
        modelAndView.addObject("deploymentList", deploymentList);
        return modelAndView;
    }

    /**
     * 获取待办任务/历史任务
     */
    @RequestMapping(value = "/viewPersonalRelatedProcessInfo")
    public ModelAndView viewPersonalRelatedProcessInfo(String userId) {
        // 用户列表
        List<User> userList = userService.getAllUser();
        // 我的待办任务
        List<TaskVo> taskVoList = activitiService.getPersonalTaskList(userId);
        // 我的历史任务
        List<HistoricTaskVo> historyTaskVoList = activitiService.getPersonalHistoryTaskList(userId);
        ModelAndView modelAndView = new ModelAndView("process");
        modelAndView.addObject("user",
                userList.stream().filter(user -> user.getId().equals(userId)).collect(Collectors.toList()).get(0));
        modelAndView.addObject("userList", userList);
        modelAndView.addObject("taskVoList", taskVoList);
        modelAndView.addObject("historyTaskVoList", historyTaskVoList);
        return modelAndView;
    }

    /**
     * 审核流程组
     */
    @RequestMapping(value = "/submitReimbursement")
    public ModelAndView submitReimbursement(String userId, ReimbursementVo reimbursementVo,
            RedirectAttributesModelMap redirectAttributesModelMap) {
        activitiService.submitReimbursement(userId, reimbursementVo);
        redirectAttributesModelMap.addAttribute("userId", userId);
        return new ModelAndView(new RedirectView("/viewPersonalRelatedProcessInfo"));
    }

    @RequestMapping(value = "/reimbursementSubmiterCompleteTask")
    public ModelAndView reimbursementSubmiterCompleteTask(String userId, String taskId, String message,
            RedirectAttributesModelMap redirectAttributesModelMap) {
        activitiService.reimbursementSubmiterCompleteTask(userId, taskId, message);
        redirectAttributesModelMap.addAttribute("userId", userId);
        return new ModelAndView(new RedirectView("/viewDeployment"));
    }

    @RequestMapping(value = "/reimbursementAccountingCompleteTask")
    public ModelAndView reimbursementAccountingCompleteTask(String userId, String taskId, boolean exceptionBill,
            boolean accept, String message, RedirectAttributesModelMap redirectAttributesModelMap) {
        activitiService.reimbursementAccountingCompleteTask(userId, taskId, exceptionBill, accept, message);
        redirectAttributesModelMap.addAttribute("userId", userId);
        return new ModelAndView(new RedirectView("/viewDeployment"));
    }

    @RequestMapping(value = "reimbursementManagerCompleteTask")
    public ModelAndView reimbursementManagerCompleteTask(String userId, String taskId, boolean accept, String message,
            RedirectAttributesModelMap redirectAttributesModelMap) {
        activitiService.reimbursementManagerCompleteTask(userId, taskId, accept, message);
        redirectAttributesModelMap.addAttribute("userId", userId);
        return new ModelAndView(new RedirectView("/viewDeployment"));
    }

}
